﻿using ClosedXML.Excel;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using WTAppletProjectHTTPS.Models;

namespace WTAppletProjectHTTPS.Controllers
{
    [JwtAuthentication]
    public class ExcelExportController : ApiController
    {
        
        public class BackValue
        {
            public string TaskId { get; set; }//任务ID 
        }
        //POST api/<controller>
        public HttpResponseMessage Post([FromBody] ExcelTableList TL)
        {
            ReturnObject RO = new ReturnObject();
            BackValue BV = new BackValue();
            try
            {
                var taskId = Guid.NewGuid().ToString();
                TL.TaskId = taskId;
                BV.TaskId = taskId;
                var task = new ExcelTask
                {
                    TaskId = taskId,
                    Status = WTAppletProjectHTTPS.Models.TaskStatus.Pending
                };
                ExcelTaskQueue.Tasks.TryAdd(taskId, task);
                ExcelTableList TLNew = WhereStr(TL);
                TLNew.HC = HttpContext.Current;
                Task.Run(() => ExcelFile.ExcelExportFile(TLNew));

                RO.Status = HttpStatusCode.OK;
                RO.Message = "OK";
                RO.Data = BV;
                return Request.CreateResponse(HttpStatusCode.OK, RO);
            }
            catch (Exception ex)
            {
                RO.Status = HttpStatusCode.NotFound;
                RO.Message = "ERROR:"+ex.Message;
                RO.Data = null;
                return Request.CreateResponse(HttpStatusCode.NotFound, RO);
            }
            
        }

        
        public static ExcelTableList WhereStr(ExcelTableList TL)
        {
            var whereClause = "WHERE 1=1";
            if (TL.DateRange == "今日")
            {
                whereClause += $" AND HeadDate between '{DateTime.Now.Date.ToString("yyyy-MM-dd")} 00:00:00' and '{DateTime.Now.Date.ToString("yyyy-MM-dd")} 23:59:59'";
                TL.StartDate = DateTime.Now.Date.ToString("yyyy-MM-dd");
                TL.EndDate = DateTime.Now.Date.ToString("yyyy-MM-dd");
            }
            else if (TL.DateRange == "昨日")
            {
                whereClause += $" AND HeadDate between '{DateTime.Now.Date.AddDays(-1).ToString("yyyy-MM-dd")} 00:00:00' and '{DateTime.Now.Date.AddDays(-1).ToString("yyyy-MM-dd")} 23:59:59'";
                TL.StartDate = DateTime.Now.Date.AddDays(-1).ToString("yyyy-MM-dd");
                TL.EndDate = DateTime.Now.Date.AddDays(-1).ToString("yyyy-MM-dd");
            }
            else if (TL.DateRange == "本周")
            {
                // 获取当前日期
                DateTime today = DateTime.Today;

                // 计算本周的开始日期（周一）
                DateTime startOfWeek = today.AddDays(-(int)today.DayOfWeek + (int)DayOfWeek.Monday);

                // 计算本周的结束日期（周日）
                DateTime endOfWeek = startOfWeek.AddDays(6);
                whereClause += $" AND HeadDate between '{startOfWeek.ToString("yyyy-MM-dd")} 00:00:00' and '{endOfWeek.ToString("yyyy-MM-dd")} 23:59:59'";
                TL.StartDate = startOfWeek.ToString("yyyy-MM-dd");
                TL.EndDate = endOfWeek.ToString("yyyy-MM-dd");
            }
            else if (TL.DateRange == "上周")
            {
                // 获取当前日期
                DateTime today = DateTime.Today;

                // 计算本周的开始日期（周一）
                DateTime startOfWeek = today.AddDays(-(int)today.DayOfWeek + (int)DayOfWeek.Monday);

                // 计算上周的开始日期（上周一）
                DateTime startOfLastWeek = startOfWeek.AddDays(-7);

                // 计算上周的结束日期（上周日）
                DateTime endOfLastWeek = startOfLastWeek.AddDays(6);
                whereClause += $" AND HeadDate between '{startOfLastWeek.ToString("yyyy-MM-dd")} 00:00:00' and '{endOfLastWeek.ToString("yyyy-MM-dd")} 23:59:59'";
                TL.StartDate = startOfLastWeek.ToString("yyyy-MM-dd");
                TL.EndDate = endOfLastWeek.ToString("yyyy-MM-dd");
            }
            else if (TL.DateRange == "本月")
            {
                // 获取当前日期
                DateTime today = DateTime.Today;

                // 计算本月的第一天
                DateTime firstDayOfMonth = new DateTime(today.Year, today.Month, 1);

                // 计算本月的最后一天
                DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);
                whereClause += $" AND HeadDate between '{firstDayOfMonth.ToString("yyyy-MM-dd")} 00:00:00' and '{lastDayOfMonth.ToString("yyyy-MM-dd")} 23:59:59'";
                TL.StartDate = firstDayOfMonth.ToString("yyyy-MM-dd");
                TL.EndDate = lastDayOfMonth.ToString("yyyy-MM-dd");
            }
            else if (TL.DateRange == "上月")
            {
                // 获取当前日期
                DateTime today = DateTime.Today;

                // 计算上个月的第一天
                DateTime firstDayOfLastMonth = new DateTime(today.Year, today.Month, 1).AddMonths(-1);

                // 计算上个月的最后一天
                DateTime lastDayOfLastMonth = new DateTime(today.Year, today.Month, 1).AddDays(-1);
                whereClause += $" AND HeadDate between '{firstDayOfLastMonth.ToString("yyyy-MM-dd")} 00:00:00' and '{lastDayOfLastMonth.ToString("yyyy-MM-dd")} 23:59:59'";
                TL.StartDate = firstDayOfLastMonth.ToString("yyyy-MM-dd");
                TL.EndDate = lastDayOfLastMonth.ToString("yyyy-MM-dd");
            }
            else if (TL.DateRange == "今年")
            {
                // 获取当前日期
                DateTime today = DateTime.Today;

                // 计算今年的第一天
                DateTime firstDayOfYear = new DateTime(today.Year, 1, 1);

                // 计算今年的最后一天
                DateTime lastDayOfYear = new DateTime(today.Year, 12, 31);
                whereClause += $" AND HeadDate between '{firstDayOfYear.ToString("yyyy-MM-dd")} 00:00:00' and '{lastDayOfYear.ToString("yyyy-MM-dd")} 23:59:59'";
                TL.StartDate = firstDayOfYear.ToString("yyyy-MM-dd");
                TL.EndDate = lastDayOfYear.ToString("yyyy-MM-dd");
            }
            else if (TL.DateRange == "去年")
            {
                // 获取当前日期
                DateTime today = DateTime.Today;

                // 计算去年的第一天
                DateTime firstDayOfLastYear = new DateTime(today.Year - 1, 1, 1);

                // 计算去年的最后一天
                DateTime lastDayOfLastYear = new DateTime(today.Year - 1, 12, 31);
                whereClause += $" AND HeadDate between '{firstDayOfLastYear.ToString("yyyy-MM-dd")} 00:00:00' and '{lastDayOfLastYear.ToString("yyyy-MM-dd")} 23:59:59'";
                TL.StartDate = firstDayOfLastYear.ToString("yyyy-MM-dd");
                TL.EndDate = lastDayOfLastYear.ToString("yyyy-MM-dd");
            }
            else if (TL.DateRange == "近7天")
            {
                whereClause += $" AND HeadDate between '{DateTime.Now.Date.AddDays(-6).ToString("yyyy-MM-dd")} 00:00:00' and '{DateTime.Now.Date.ToString("yyyy-MM-dd")} 23:59:59'";
                TL.StartDate = DateTime.Now.Date.AddDays(-6).ToString("yyyy-MM-dd");
                TL.EndDate = DateTime.Now.Date.ToString("yyyy-MM-dd");
            }
            else if (TL.DateRange == "近30天")
            {
                whereClause += $" AND HeadDate between '{DateTime.Now.Date.AddDays(-29).ToString("yyyy-MM-dd")} 00:00:00' and '{DateTime.Now.Date.ToString("yyyy-MM-dd")} 23:59:59'";
                TL.StartDate = DateTime.Now.Date.AddDays(-29).ToString("yyyy-MM-dd");
                TL.EndDate = DateTime.Now.Date.ToString("yyyy-MM-dd");
            }
            else if (TL.DateRange == "自定义")
            {
                whereClause += $" AND HeadDate between '{TL.StartDate} 00:00:00' and '{TL.EndDate} 23:59:59'";
            }
            else if (TL.DateRange == "全部")
            {
                whereClause += "";
                TL.StartDate = "全部";
            }

            switch (TL.OpeationType)
            {
                case "D574 成品关联模组生产日期":
                    whereClause += " and HeadFormName = '成品关联模组生产日期'";
                    TL.OpeationType = "D574成品关联模组生产日期";
                    break;
                case "D553 模组测试站换型物料自检确认":
                    whereClause += " and HeadFormName = '测试站换型物料确认'";
                    TL.OpeationType = "D553模组测试站换型物料自检确认";
                    break;
                case "D554 扣边框站换型物料自检确认(手工)":
                    whereClause += " and HeadFormName = '扣边框站换型物料确认'";
                    TL.OpeationType = "D554扣边框站换型物料自检确认(手工)";
                    break;
                case "D555 镭雕站换型自检确认":
                    whereClause += " and HeadFormName = '镭雕站换型自检确认'";
                    TL.OpeationType = "D555镭雕站换型自检确认";
                    break;
                case "D556 包装工位信息确认":
                    whereClause += " and HeadFormName = '包装站换型信息确认'";
                    TL.OpeationType = "D556包装工位信息确认";
                    break;
                case "D560 例行检验(生产全检记录)":
                    whereClause += " and HeadFormName = 'Cola 生产全检记录'";
                    TL.OpeationType = "D560例行检验(生产全检记录)";
                    break;
                case "D558 Cola 首件/巡检记录":
                    whereClause += " and HeadFormName = 'Cola首件/巡检记录'";
                    TL.OpeationType = "D558Cola 首件巡检记录";
                    break;
                case "D559 Cola 终检检验记录":
                    whereClause += " and HeadFormName = 'Cola终检检验记录'";
                    TL.OpeationType = "D559Cola 终检检验记录";
                    break;
                case "D587 Cola产品功能测试":
                    whereClause += " and HeadFormName = 'Cola 产品功能测试'";
                    TL.OpeationType = "D587Cola 产品功能测试";
                    break;
                case "D562 包装后全检记录":
                    whereClause += " and HeadFormName = 'Cola产线包装后全检'";
                    TL.OpeationType = "D562包装后全检记录";
                    break;
                case "D595 QC抽检成品边框":
                    whereClause += " and HeadFormName = 'QC抽检成品边框'";
                    TL.OpeationType = "D595QC抽检成品边框";
                    break;
                case "D594 A1/A2员工边框上料确认(自动)":
                    whereClause += " and HeadFormName = 'A1/A2员工边框上料确认'";
                    TL.OpeationType = "D594A1/A2员工边框上料确认(自动)";
                    break;
                case "全部":
                    whereClause += "";
                    TL.OpeationType = "全部";
                    break;
                default:
                    break;
            }
            TL.WhereStrValue = whereClause;

            return TL;
        }
    }
}